﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using PropertyManagementSystemAPI.Common;
using PropertyManagementSystemAPI.Common.RedisHelper;
using PropertyManagementSystemAPI.Common.TreeTable;
using StackExchange.Redis;

namespace PropertyManagementSystemAPI.Controllers
{
    /// <summary>
    /// 权限管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class JurisdictionManageController : ControllerBase
    {
        private readonly IFreeSql db;
        //依赖注入redis
        public readonly IDatabase rediusDB;
        public JurisdictionManageController(IFreeSql db, RedisBase _redisBase)
        {
            this.db = db;
            this.rediusDB = _redisBase.GetDatabase();
        }
        /// <summary>
        /// 获取树形表格数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public IActionResult getinfo(int roleid)
        {
            List<Models.Systemresourcemodule> list = db.Select<Models.Systemresourcemodule>()
                .Where(x => x.Isdelete == false)
                .ToList();
            var info = new getRoleJurisdictionTreeList().GetSystemResourceModList(db, list,roleid);
            return Ok(info);
        }

        /// <summary>
        /// 获取菜单信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("getMenuInfo")]
        public IActionResult getMenuInfo()
        {
            string systemLoginInfo = rediusDB.StringGet("sysLoginInfo");
            if (systemLoginInfo == null)
            {
                return Ok(null);
            }

            MySqlParameter parameter = new MySqlParameter("@id", MySqlDbType.Int32);
            parameter.Value = JsonConvert.DeserializeObject<Models.Systeminfo>(systemLoginInfo).Sysid;
            string sql = "select a.* from systemresourcemodule a " +
                "join roleresourcemodule b on a.id=b.resourcemoduleld " +
                "join roleinfo c on c.roleid = b.roleid " +
                "join userrole d on d.roleid = c.roleid " +
                "join systeminfo e on e.sysid = d.userid where a.isdelete = 0 and e.sysid = @id";
            List<Models.Systemresourcemodule> list = db.Select<Models.Systemresourcemodule>().WithSql(sql, parameter).ToList();

            var resList = new getTreeTableData().GetSystemResourceModList(db, list);
            return Ok(resList);
        }

        /// <summary>
        /// 获取报表头部展示区所需数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("getNumber")]
        public IActionResult getNumber()
        {
            decimal list1 = db.Select<Models.Parkingspaceinfo>()
                .Where(x => x.Pdelete == false)
                .Sum(x => x.Pid);
            decimal list2 = db.Select<Models.Houseinfo>()
                .Where(x => x.IsDelete == false)
                .Sum(x => x.Hid);
            decimal list3 = db.Select<Models.Ownerinfo>()
                .Where(x => x.Isdelete == false)
                .Sum(x => x.Oid);
            var json = new
            {
                carnumer = list1,
                housenum = list2,
                ownernumber = list3,
            };
            return Ok(json);
        }
    }
}
